package com.book.store.service.store.endpoint;

import com.book.store.domain.store.dto.Greeting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

@RestController
public class IndexController {

    @Autowired
    private ApplicationContext appContext;

    @RequestMapping("")
    public String index(){
        return String.format("%s running %s",appContext.getId(),new Date(appContext.getStartupDate()));
    }

    private static final String template = "Hello, %s! I'm from %s";
    private final AtomicLong counter = new AtomicLong();

    @Value("${server.port}")
    private int serverPort = 0;

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name, serverPort));
    }
}
